package com.woniuxy.meal.controller.consumer;

import com.woniuxy.meal.common.RedisKey;
import com.woniuxy.meal.entity.DietOrderFood;
import com.woniuxy.meal.form.message.ChangeSaleMessage;
import com.woniuxy.meal.util.RedisUtil;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
@RocketMQMessageListener(consumerGroup = "sale",topic = "change_sale")
public class SaleConsumer implements RocketMQListener<ChangeSaleMessage> {

    @Autowired
    private RedisUtil redisUtil;


    @Override
    public void onMessage(ChangeSaleMessage msg) {
        List<DietOrderFood> list = msg.getList();

        for (DietOrderFood dietOrderFood : list) {
            String foodSaleKey = RedisKey.getFoodSaleKey();
            try {
//                System.out.println(dietOrderFood.getCount());
                redisUtil.zincrby(foodSaleKey,dietOrderFood.getFoodId().toString(),dietOrderFood.getCount());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
}
